# This is the Standard Library for all common used functions in Automated Testing
# New functions should be defined by self.<FunctionName>. This allows it to be called by CRM.<FunctionName>
require "win32ole"
require "lib/std_lib.rb"
require "watir"
require "watir/testcase"
require "watir/ie"
#module CRM_STD_LIB
#=begin
class EBS_STD_LIB
  
    # This is for 'reates a new browser and accesses the URL passed',can be called by EBS.new()
    $test = "https://oracletest.activenetwork.com/OA_HTML/RF.jsp?function_id=28716"
    $product = "https://oracle.activenetwork.com/OA_HTML/RF.jsp?function_id=28716"
    $date = "#{Time.now.strftime("%m/%d/%Y")}"
    $username = "TCHEN1"
    $password = "2165270qaz"

    def initialize(url)
        $ie = Watir::IE.start(url)
        # Sets the Test Speed to fast
        $ie.set_fast_speed   
    end
    #    $nie=$ie.div(:id,"Global Search Multi FieldDiv")
    # This is for 'close the test browser when finish testing', can be called by CRM.Close()
    def self.close()
        $ie.close()
    end

    # This is for 'Login testing website', can be called by CRM.login()
    def self.login(url,user, pwd)
        begin
            $ie = Watir::IE.start(url)
            $ie.maximize
            if $ie.link(:name,'overridelink').exist?
                $ie.link(:name,'overridelink').click
            end

            sleep 5
            #            if $ie.title =="Home"

            #            else

            $ie.text_field(:name, 'usernameField').set user
            $ie.text_field(:name, 'passwordField').set pwd
                
            $ie.button(:text,"Login").click
        end
           
        #if $ie.contains_text(/Search For/)
        if $ie.title.include?("Oracle Applications")
            return true
        else
            return false
        end
    rescue #=> e
        #$log.Test_Failed('Login', e)
        puts $!
        puts $@
        $log.close unless $log.nil?
    ensure

        
    end
  
    #This is for 'Logout from testing website', can be called by CRM.Logout()
    def self.logout()
        begin
            $ie.link(:text, "Sign Out").click
      
            if $ie.title == 'Home'
                return true
            else
                # $log.Test_Failed('Logout', "Sign In Error")
                return false
            end
        rescue #=> e
            #$log.Test_Failed('Logout', e)
            puts $!
            puts $@
            $log.close unless $log.nil?
        end
    end

    def self.open_page(pagename)
        @excel=WIN32OLE.new("Excel.Application")
        @excel.visible=false
        @workbook = @excel.workbooks.open 'D:/CRM/doc/data/page_link_of_CRM.xls'
        #@workbook = @excel.workbooks.open '../../doc/data/page_link_of_CRM.xls'
        if $ie.url.include?("crmint")
            @worksheet=@workbook.worksheets(1)
        else
            @worksheet=@workbook.worksheets(2)
        end
        #@sheet = excel.activesheet
        @range = @worksheet.Range("C1:C68").value

        @pagename = pagename
        def self.page_link(test)
            @test = test
            @test = @pagename
            for i in 0..67
                if @range[i].to_s==@test
                    break
                else
                    i+=1
                end
            end
            return @worksheet.Cells(i+1,4).value.to_s
        end
        @pagelink = page_link(@test)
        $ie.goto(@pagelink)
        @workbook.close
        @excel.Quit()

    end
    def self.popup_clicker(ie,buttonname)
        hwnd = ie.enabled_popup(10)
        if(hwnd)      #yeah! a popup
            popup = WinClicker.new
            popup.makeWindowActive(hwnd) #Activate the window.
            popup.clickWindowsButton_hwnd(hwnd,buttonname) #Click the button
            popup=nil
        end
    end
    def page_dives
        $quick_search = $ie.div(:id,"Global Search Multi FieldDiv")
        $quick_create = $ie.div(:id,"Quick CreateDiv")
        $header_links = $ie.div(:id,"header")
        $header_tabs = $ie.div(:id,"tabs")
    end
    # count same elements of the page (Need to modify)
    #    def self.count_numbers(element)
    #        for i in 1..10000
    #            if element.exist?
    #                i += 1
    #            else
    #                return i-1
    #            end
    #        end
    #    end
end
#end
#=end